package cn.jiahua.hospital.controller.adminBack;

import cn.jiahua.hospital.dto.Page;
import cn.jiahua.hospital.dto.Result;
import cn.jiahua.hospital.pojo.News;
import cn.jiahua.hospital.pojo.User;
import cn.jiahua.hospital.service.NewsService;
import cn.jiahua.hospital.utils.ResultUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@Api(tags = "超级管理员后台-新闻")
@RequestMapping("/api/back/news/")
public class NewsBackController {
    @Autowired
    NewsService newsService;

    @ApiOperation(value = "新闻分页", produces = "application/json", notes = "根据作者和标题进行模糊查询")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "newsTitle", value = "新闻标题", required = false, dataType = "String"),
            @ApiImplicitParam(name = "newsAuthor", value = "作者", required = false, dataType = "String"),
            @ApiImplicitParam(name = "page", value = "当前页码 ", required = true, dataType = "int"),
            @ApiImplicitParam(name = "limit", value = "页大小 ", required = true, dataType = "int")
    })
    @GetMapping("/limit")
    public Result limit(String newsTitle,
                        String newsAuthor,
                        Integer page,
                        Integer limit) {
        //总数量
        int count = newsService.newsSelectCount(newsTitle, newsAuthor);
        System.out.println(count);
        Page<User> page1 = new Page<User>();
        page1.setTotal(count);
        page1.setPageNo(page);
        page1.setPageSize(limit);
        //总页数

        int pageCount=page1.getPages();

        /*String sort="newsCreateTime";*/
        //调用service层获取数据
        List<News> newsList=newsService.newsSelectPage(newsTitle,newsAuthor,(page-1)*limit,limit);


        Map<String, Object> map = new HashMap<String, Object>();
        map.put("page1", page1);
        map.put("newsList", newsList);
        return ResultUtils.returnSuccess("查询成功", map);
    }

    @ApiOperation(value = "查看新闻", produces = "application/json", notes = "根据新闻ID查看新闻详细信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "newsId", value = "新闻ID", required = true, dataType = "int")
    })
    @GetMapping("/select")
    public Result select(Integer newsId) {
        News news = newsService.getNewsById(newsId);
        return ResultUtils.returnDataSuccess(news);
    }

    @ApiOperation(value = "删除新闻", produces = "application/json", notes = "根据新闻ID删除新闻信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "newsId", value = "新闻ID", required = true, dataType = "int")
    })
    @GetMapping("/delete")
    public Result delete(Integer newsId) {
        int count = newsService.qdtxDeleteNewsById(newsId);
        if (count > 0) {
            return ResultUtils.returnSuccess("删除成功");
        }
        return ResultUtils.returnFail();
    }


    @ApiOperation(value = "增加新闻", produces = "application/json", notes = "增加新闻信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "newsTitle", value = "新闻标题", required = true, dataType = "String"),
            @ApiImplicitParam(name = "newsContent", value = "新闻内容", required = true, dataType = "String"),
            @ApiImplicitParam(name = "newsAuthor", value = "作者", required = true, dataType = "String")
    })
    @PostMapping("/add")
    public Result add(String newsTitle,String newsContent,String newsAuthor) {
        News news = new News();
        news.setNewsAuthor(newsAuthor);
        news.setNewsContent(newsContent);
        news.setNewsTitle(newsTitle);
        news.setNewsCreateTime(new Date());
        int count = newsService.qdtxAddNews(news);
        if (count > 0) {
            return ResultUtils.returnSuccess("增加成功");
        }
        return ResultUtils.returnFail();
    }

    @ApiOperation(value = "修改新闻", produces = "application/json", notes = "根据新闻ID修改新闻信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "newsId", value = "新闻ID", required = true, dataType = "int"),
            @ApiImplicitParam(name = "newsTitle", value = "新闻标题", required = true, dataType = "String"),
            @ApiImplicitParam(name = "newsContent", value = "新闻内容", required = true, dataType = "String"),
            @ApiImplicitParam(name = "newsAuthor", value = "作者", required = true, dataType = "String")
    })
    @PostMapping("/modify")
    public Result add(Integer newsId,String newsTitle,String newsContent,String newsAuthor) {
        News news = new News();
        news.setNewsId(newsId);
        news.setNewsAuthor(newsAuthor);
        news.setNewsContent(newsContent);
        news.setNewsTitle(newsTitle);
        news.setNewsModifyTime (new Date());
        int count = newsService.qdtxModifyNews(news);
        if (count > 0) {
            return ResultUtils.returnSuccess("修改成功");
        }
        return ResultUtils.returnFail();
    }
}